/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.Usuario;

import Model.User;
import com.google.gson.Gson;
import java.util.List;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.hibernate.Session;

/**
 *
 * @author Radek
 */
@Path("/altera")
public class Altera {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public Response sayHello(User usuario) {
        Session session = null;
        List<User> result;
        Gson gson = new Gson();
        String response;
        try {
            session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();
            result = session.createQuery("From User WHERE cod=? ")
                    .setString(0, usuario.getCod().toString())
                    .list();
            HibernateUtil.commitTransaction();
            response = gson.toJson(result.get(0));

        } catch (Exception e) {
            System.out.println(e);
            response = "Error";
            session.getTransaction().rollback();
        } finally {
            try {
                HibernateUtil.closeSession();
            } catch (Throwable e) {
                System.out.println(e.getMessage());
            }
        }
        return Response.status(201).entity(response).build();
    }
}
